*******************************************************************************
* Load the candidate data
*******************************************************************************

use dataset_candidates, clear
keep if !mi(ideology)

*******************************************************************************
* Tabulate experience
*******************************************************************************

preserve

	keep otherp ideology winner
	collapse (count) N=ideology (mean) ideology (mean) winner, by(otherp)
	format ideology %20.02fc
	format winner %20.02fc
	
*******************************************************************************
* Output
*******************************************************************************

	#delimit;

	listtex 
		using "_output/tableS5_a.tex"
		,
		replace
		type rstyle(tabular)
		head(
			\begin{tabular*}
			{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{4}{c}}
			\toprule 
			 Experience & N & Mean Ideology & 
			 Pr(Winner)
			 \\		 
			\midrule
		)
		foot("\bottomrule\end{tabular*}")
		;
		
	#delimit cr

restore

*******************************************************************************
* Alt coding
*******************************************************************************

encode otherp, gen(deputyc)
replace deputy = 1 if deputyc >= 3

*******************************************************************************
* Do the ideology regressions with alt coding
*******************************************************************************

eststo clear
eststo: reg ideology deputy, cl(district)
eststo: reg ideology deputy trumpvoteshare, cl(district)
eststo: areg ideology deputy cycle_*, cl(district) a(state)
estadd local cyclefe = "X"
estadd local statefe = "X"
eststo: areg ideology deputy trumpvoteshare cycle_*, cl(district) a(state)
estadd local cyclefe = "X"
estadd local statefe = "X"

label variable deputy "Experienced"
label variable trumpvoteshare "Trump Vote Share"

#delimit;

esttab
	using "_output/tableS5_b.tex"
	,
		replace
		booktabs
		cells(b(fmt(3) star) se(par))
		drop(cycle*) 
		stats(
			N cyclefe statefe,
			label(
				"Observations"
				"Cycle fixed effects"
				"State fixed effects"
			)
			fmt(%20.0f %20.0f)
		)
		label
		collabels(none)	
		mlabels(none)
		width(\linewidth)
		alignment(l)
		gap
		;
		
#delimit cr

*******************************************************************************
* Do the election regressions with alt coding
*******************************************************************************

eststo clear

eststo: areg voteshare deputy cycle_* i.ncandidates, cl(district) a(state)
eststo: areg voteshare deputy ideology cycle_* i.ncandidates, ///
	cl(district) a(state)
eststo: areg voteshare deputy ideology incumbent cycle_* i.ncandidates, ///
	cl(district) a(state)

eststo: areg winner deputy cycle_* i.ncandidates, cl(district) a(state)
eststo: areg winner deputy ideology cycle_* i.ncandidates, ///
	cl(district) a(state)
eststo: areg winner deputy ideology incumbent cycle_* i.ncandidates, ///
	cl(district) a(state)

*******************************************************************************
* Output
*******************************************************************************

label variable deputy "Experienced"
label variable incumbent "Incumbent"
label variable ideology "Candidate Conservatism"

#delimit;

esttab
	using "_output/tableS5_c.tex"
	,
		replace
		booktabs
		cells(b(fmt(3)) se(par) p(par("[" "]")))
		drop(cycle* *cand* _cons) 
		stats(
			N,
			label(
				"Observations"
			)
			fmt(%20.0f %20.0f)
		)
		label
		collabels(none)	
		mlabels(none)
		width(\linewidth)
		alignment(l)
		gap
		mgroups(
			"DV=Vote Share" "DV=Elected", pattern(1 0 0 1 0 0) 
			prefix(\multicolumn{@span}{c}{) suffix(}) 
			span 
			erepeat(\cmidrule(lr){@span})
		)		
		;
		
#delimit cr

*******************************************************************************
* End
*******************************************************************************
